package abq2.jic.jic.plugin.operate.activity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于查询工作招聘信息的插件
 */
public class GetActivityInfo implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        //如果操作名称相等
        if ("GET_Activity_Info".equalsIgnoreCase(action)) {
            //获取DynamicObject列表，将要获取的字段信息注入
            //将招聘信息的字段加载出来
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_activity",
                    "name," + //活动名称
                            "abq2_type," +   //活动类型
                            "abq2_admin," +  //活动管理员
                            "abq2_telephone," +  //活动管理员电话
                            "abq2_method," +  //考勤标准
                            "abq2_introduce," +  //介绍
                            "abq2_place," +  //地点
                            "abq2_integerfield," + //活动分
                            "abq2_limit," + //限制人数
                            "abq2_integerfield1," + //已报人数
                            "abq2_timebegin1,abq2_time_end1,abq2_timebegin2,abq2_time_end2",
                    (new QFilter("name", QCP.is_notnull, null)).toArray());
            //创建一个JsonArray，将招聘信息加入到其中
            JSONArray jsonArray = new JSONArray();
            for (DynamicObject dynamicObject : dys) {
                Date abq2TimeEnd1 = dynamicObject.getDate("abq2_time_end1");
                int i = abq2TimeEnd1.compareTo(new Date());
                if (i != 1){
                    continue;
                }
                if (dynamicObject.getInt("abq2_limit") < dynamicObject.getInt("abq2_integerfield1")){
                    continue;
                }
                //将每一个书籍的信息加入JSONArray
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("activityName", dynamicObject.getString("name"));
                jsonObject.put("Type", dynamicObject.getString("abq2_type"));
                jsonObject.put("admin", dynamicObject.getString("abq2_admin.name"));
                jsonObject.put("telephone", dynamicObject.getString("abq2_telephone"));
                jsonObject.put("method", dynamicObject.getString("abq2_method"));
                jsonObject.put("introduce",dynamicObject.getString("abq2_introduce"));
                jsonObject.put("place",dynamicObject.getString("abq2_place"));
                jsonObject.put("score",dynamicObject.getInt("abq2_integerfield"));
                jsonObject.put("limitNum",dynamicObject.getInt("abq2_limit"));
                jsonObject.put("already",dynamicObject.getInt("abq2_integerfield1"));
                jsonObject.put("enrollTimeBegin",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dynamicObject.getDate("abq2_timebegin1")));
                jsonObject.put("enrollTimeEnd",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dynamicObject.getDate("abq2_time_end1")));
                jsonObject.put("activityTimeBegin",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dynamicObject.getDate("abq2_timebegin2")));
                jsonObject.put("activityTimeEnd",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dynamicObject.getDate("abq2_time_end2")));
                jsonArray.add(jsonObject);
            }
            System.out.println(jsonArray.toJSONString());
            //加入resultDynamicObject参数，将JsonArray加入到这个参数当中，然后返回
            result.put("activityInfo", jsonArray.toJSONString());
        }
        return result;
    }

}